﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
   
    /// <summary>
    /// 筛查项目
    /// </summary>
    [RoutePrefix("api/ScreenPropaganda")]
    public class ScreenPropagandaController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 筛查项目逻辑
        /// </summary>
        public ScreenPropagandaBll propagandaBll { get; set; }

        /// <summary>
        /// 创建宣教
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// </summary>
        /// <param name="propaganda">宣教对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Post([FromUri] ScreenPropaganda propaganda)
        {
            try
            {
                ResResult resResult = propagandaBll.CreatePropaganda(propaganda);
                return Ok<ResResult>(resResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 更新筛查宣教
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// </summary>
        /// <param name="propaganda">筛查宣教对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Put([FromUri] ScreenPropaganda propaganda)
        {
            try
            {
                if (!Guid.Empty.Equals(propaganda.new_propagandaId))
                {
                    ResResult resResult = propagandaBll.UpdatePropaganda(propaganda);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID，潜客ID查询筛查宣教信息
        /// 原程序方法：ScreeningServiceImpl-> queryPreach(String scid, String customerid)
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryPropagandaByScidAndLeadId")]
        public IHttpActionResult QueryPropagandaByScidAndLeadId(Guid scid, Guid leadid)
        {
            try
            {
                IEnumerable<ScreenPropagandaView> result = propagandaBll.QueryPropagandaByScidAndLeadId( scid,  leadid);
                return Ok<IEnumerable<ScreenPropagandaView>>(result);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

    }
}
